package com.app.fenak.asynctasks;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.app.fenak.LoginActivity;
import com.app.fenak.validation.ServicesUrls;

import android.os.AsyncTask;
import android.util.Log;

public class LoginAsyncTask extends AsyncTask<Object, Object, Object> {

	private LoginActivity loginActivity;

	@Override
	protected Object doInBackground(Object... params) {

		loginActivity = (LoginActivity) params[0];
		String email = (String) params[1];
		String password = (String) params[2];
//		boolean loginSuccess = false ;

		try {

			ArrayList<NameValuePair> requestString = new ArrayList<NameValuePair>();
			requestString.add(new BasicNameValuePair(
					ServicesUrls.LOGIN_PARAM_USER_EMAIL, email));
			requestString.add(new BasicNameValuePair(
					ServicesUrls.LOGIN_PARAM_PASSWORD, password));

			HttpClient httpClient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(ServicesUrls.LOGIN_URL);
			httpPost.setEntity(new UrlEncodedFormEntity(requestString));
			Log.i("request ", httpPost.getRequestLine().toString());

			HttpResponse response = httpClient.execute(httpPost);
			Log.i("response", response.getStatusLine().toString());
			String resonseString = ServicesUrls.convertStreamToString(response
					.getEntity().getContent());
			Log.i("response ", resonseString);
//			JSONObject jsonObject = new JSONObject(resonseString);
//			loginSuccess = jsonObject.getBoolean("post");
//			Log.i(" login success", loginSuccess + "");

		} catch (UnsupportedEncodingException e) {

			Log.e("UnsupportedEncodingException", e.getMessage());
		} catch (IOException e) {

			Log.e("IOException", e.getMessage());

		}
//		catch (JSONException e) {
//
//			Log.e("JSONException", e.getMessage());
//		}

		return null;
	}
	
	@Override
	protected void onPostExecute(Object result) {
		
		super.onPostExecute(result);
		loginActivity.dismissProgressDialog();
	}

}
